package me.jessyan.mvparms.demo.mvp.ui.activity.integral;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.xrecyclerview.XRecyclerView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.integral.DaggerMyExchangeComponent;
import me.jessyan.mvparms.demo.di.module.integral.MyExchangeModule;
import me.jessyan.mvparms.demo.mvp.contract.integral.MyExchangeContract;
import me.jessyan.mvparms.demo.mvp.presenter.integral.MyExchangePresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.MyExchangeAdapter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 我的兑换页面
 *
 */
public class MyExchangeActivity extends BaseActivity<MyExchangePresenter> implements MyExchangeContract.View {
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @Inject
    @Named("barCodePop")
    CustomPopupWindow barCodePop;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMyExchangeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .myExchangeModule(new MyExchangeModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_my_exchange; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        //todo 几种状态
        initRecycler();
        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });

        barCodePop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });
    }

    private void initRecycler() {
        List<Boolean> list = new ArrayList<>();
        list.add(true);
        list.add(false);
        list.add(false);
        list.add(false);
        list.add(true);
        list.add(false);
        list.add(false);
        list.add(true);
        list.add(false);
        MyExchangeAdapter adapter = new MyExchangeAdapter(list);
        LinearLayoutManager lr = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(lr);
        ArmsUtils.configRecyclerView(recyclerView, lr);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                if (viewType != 1) {
                    if (view instanceof TextView) {
                        if (((TextView) view).getText().equals("领取")) {

                           barCodePop.show();
                           backgroundAlpha(0.7f);
                        }
                    } else {
                        Intent intent = new Intent(MyExchangeActivity.this, GiftDetailsActivity.class);
                        intent.putExtra("type", 2);
                        startActivity(intent);
                    }


                }
            }
        });


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public MyExchangeActivity getActivity() {
        return this;
    }

    @Override
    public void dismissPop() {
        if (barCodePop != null && barCodePop.isShowing()) {
            barCodePop.dismiss();
        }
    }
}
